/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef FEATURERECOGNITIONMODEL_H
#define FEATURERECOGNITIONMODEL_H

#include "featuremodel.h"
#include "backgroundtask.h"

class FeatureRecognitionModel;
class EtalonModel;
class ColorImage; // TODO

/**
 * Background task for indexing areas
 */
class FeatureRecognitionTask : public BackgroundTask
{

    Q_OBJECT

public:

    /**
     * Constructor
     * @param model model
     */
    FeatureRecognitionTask(FeatureRecognitionModel &model, QObject *parent = 0);

protected:

    /**
     * Background task method
     */
    virtual void doRun();

    /**
     * Background task cancel method
     */
    virtual void doCancel();

private:

    /**
     * Model
     */
    FeatureRecognitionModel& _model;
};

/**
 * Feature recognition model
 */
class FeatureRecognitionModel : public FeatureModel
{

    Q_OBJECT

public:

    /**
     * Feature type
     */
    enum FeatureID {
        EImage = 0,             // image TODO        
        EEtalon,                // etalon
        ERecognizedType,        // recognize type
        ETODO,                  // TODO
        ELastFeature
    };

    /**
     * Constructor
     */
    explicit FeatureRecognitionModel(QObject *parent = 0);

    /**
     * Recognize features
     * @return features recognization task
     */
    FeatureRecognitionTask* recognizeFeatures();

    /**
     * Gets etalon model
     * @returns etalon model
     */
    EtalonModel& etalonModel();

    /**
     * Updates etalon from features
     * @param row feature row
     */
    void updateEtalon(const int row);

    /**
     * Recognizes types
     * @param row feature row
     */
    void recognize(const int row);

// From QAbstractItemModel

    Qt::ItemFlags flags(const QModelIndex &index) const;

signals:

    /**
     * Emited when etalon feature is updated
     * @param name etalon name
     * @param value etalon value
     * @param range etalon range
     */
    void etalonFeatureUpdated(QString &name, Feature &value, Feature &range);

public slots:

    /**
     * Sets features
     * @param features features
     */
    void setFeatures(const ItemTable &features);

    /**
     * Updates etalon
     */
    void updateEtalon(const QModelIndex & topLeft, const QModelIndex & bottomRight);

private:

    /**
     * Init
     */
    void init();

    /**
     * Collects etalon features
     * @param row feature row
     * @param features features
     * @returns etalon features
     */
    Features collectEtalonFeatures(const int row, FeatureTypes &types);

    /**
     * Computes feature maximum
     * @param types feature type
     * @return feature maximum
     */
    double computeMaximum(const FeatureType type);    

    /**
     * Create etalon features image
     * @param features etalon features
     * @return etalon features image
     */
    ColorImage createEtalonFeaturesImage(const Features &features);

    /**
     * Etalon model
     */
    EtalonModel *_etalonModel;

};

#endif // FEATURERECOGNITIONMODEL_H
